﻿

//========================常量命名变量，请不要更改此部分数据
var divAjaxAutoBoxClass1 = "divAjaxAutoBox";
var tbAjaxDataBoxClass1 = "tbAjaxDataBox";

var divAjaxAutoBoxPrefix6 = "jAjax_divAutoBox_3";
var tbAjaxDataBoxPrefix6 = "jAjax_tbDataBox_3";
//=========================================================



$(document).ready(function () {
    $(".txtAjaxPackAutoComplete1").bind('keyup').keyup(function (e) {
        var key = (e.keyCode) || (e.which) || (e.charCode); //兼容IE(e.keyCode)和Firefox(e.which)
        if (key != 38 && key != 40 && key != 13 && key != 9) {
            doAjaxRequest6($(this));
        }
    })

    $(".txtAjaxPackAutoComplete1").bind("focus", ShowSearchResult6);
    $(".txtAjaxPackAutoComplete1").bind("blur", CheckInputResult6);

    //AJAX传输Loading显示
    $("#divLoading").ajaxStart(function () {
        $(this).show();

    }).ajaxStop(function () {
        $(this).hide();
    });

})


//判断是否发送AJAX请求
function PrepareAjaxRequest6($text) {
    if (($text).type == 'keyup') {
        doAjaxRequest6($(this));
    }
}

//发送Ajax请求
function doAjaxRequest6($text) {
    //提交字符串的转义字符处理
    var txtValue = $text.val();
    var postData = '{EngValue:"' + txtValue + '"}';
    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "../AjaxCallbackService.asmx/GetBaseCodePack",
        data: postData,
        dataType: 'json',
        success: function (result) {
            CreateResultDataBoxHTML6($text, result);
        },
        error: function (httpException) {
            alert("数据加载失败，请重试。");
        },
        timeout: 40000
    });
}

//根据AJAX请求结果生成的数据表单并弹出
function CreateResultDataBoxHTML6($text, result) {
    var divAutoBoxID = divAjaxAutoBoxPrefix6 + $text.attr("id");
    if ($("#" + divAutoBoxID).length == 0) {
        var divAutoBoxHTML = "<div id='" + divAutoBoxID + "' class='" + divAjaxAutoBoxClass1 + "' style='display:none;'></div>";
        $text.after(divAutoBoxHTML);
    }
    else {
        $("#" + divAutoBoxID).empty();
    }

    var tbDataBoxID = tbAjaxDataBoxPrefix6 + $text.attr("id");
    var tbDataBoxHTML = "<table cellspacing='0' cellpadding='0' id='" + tbDataBoxID + "' class='" + tbAjaxDataBoxClass1 + "'>";
    if (result.d.length == 0) {
        tbDataBoxHTML = tbDataBoxHTML + "<tr><td></td></tr></table>";
    }
    else {
        $(result.d).each(function () {

            var trHTML = "<tr class='trDataRow'><td class='tdText'>" + this["EngValue"] + "<font color='blue'>[" + this["ChnValue"] + "]</td>";
            trHTML = trHTML + "<td class='tdCode' style='display:none'>" + this["Code"] + "</td>";
            trHTML = trHTML + "<td class='tdValue' style='display:none'>" + this["EngValue"] + "</td></tr>";

            tbDataBoxHTML = tbDataBoxHTML + trHTML;
        });
        tbDataBoxHTML = tbDataBoxHTML + "</table>";
    }

    var divAutoBoxID = divAjaxAutoBoxPrefix6 + $text.attr("id");

    //将数据加入到弹出提示框中
    $("#" + divAutoBoxID).append(tbDataBoxHTML);

    //弹出提示框的位置设置
    $("#" + divAutoBoxID).css({ width: $text.css("width") });
    $("#" + divAutoBoxID).css({ top: (+$text.css("height").replace("px", "") + $text.offset().top + 6), left: ($text.offset().left + 3) + "px" });
    $("#" + divAutoBoxID).show().slideDown("slow");


    var selecotr = "#" + tbDataBoxID + " tr.trDataRow";
    //绑定鼠标移动时间，设置鼠标移动行颜色      
    $(selecotr).mouseover(function () {
        $(selecotr).removeClass("trAjaxMouseover");
        $(this).addClass("trAjaxMouseover");
    })

    //绑定单击事件，设置输入框的值
    $(selecotr).click(function () {
        $(selecotr).removeClass("trAjaxSelected");
        $(this).addClass("trAjaxSelected");
        var selectedCode = $($(this).find(".tdCode")[0]).text();
        var selectedValue = $($(this).find(".tdValue")[0]).text();

        $text.val(selectedValue);
    })
    //绑定键盘事件
    $($text).unbind('keydown').keydown(function (e) {
        var key = (e.keyCode) || (e.which) || (e.charCode); //兼容IE(e.keyCode)和Firefox(e.which)
        if (key == 38) {
            $(selecotr).eq(getIndexUp(tbDataBoxID)).addClass("trAjaxMouseover");
        } else if (key == 40) {
            $(selecotr).eq(getIndexDown(tbDataBoxID)).addClass("trAjaxMouseover");
        } else if (key == 13) {
            var len = $("#" + tbDataBoxID + " tr").size();
            for (var i = 0; i < len; i++) {
                if ($(selecotr).eq(i).hasClass("trAjaxMouseover")) {
                    var selectedCode = $("#" + tbDataBoxID + " tr:eq(" + i + ") td:eq(1)").text();
                    var selectedValue = $("#" + tbDataBoxID + " tr:eq(" + i + ") td:eq(2)").text();
                    $text.val(selectedValue);
                    var divAutoBoxID = divAjaxAutoBoxPrefix6 + $(this).attr("id");
                    $("#" + divAutoBoxID).slideUp("fast");
                }
            }
        }
    })
    $("#" + divAutoBoxID).bgiframe();

}

//文本框聚焦时的处理
function ShowSearchResult6() {
    var $text = $(this);
    //第一次加载frmBookingProfile页面时的处理
    var divAutoBoxID = divAjaxAutoBoxPrefix6 + $text.attr("id");
    $("#" + divAutoBoxID).remove();
    PrepareAjaxRequest6($text);
}


function CheckInputResult6() {
    //输入框失去焦点后，隐藏数据框
    var divAutoBoxID = divAjaxAutoBoxPrefix6 + $(this).attr("id");
    $("#" + divAutoBoxID).slideUp("fast");
}

//获取当前的索引,向下
function getIndexDown(id) {
    var index = 0;
    var len = $("#" + id + " tr").size();
    for (var i = 0; i < len; i++) {
        if ($("#" + id + " tr.trDataRow").eq(i).hasClass("trAjaxMouseover")) {
            $("#" + id + " tr.trDataRow").eq(i % len).removeClass("trAjaxMouseover");
            index = i + 1;
        }
    }
    return index > (len - 1) ? 0 : (index % len);
}
//获取当前的索引,向上
function getIndexUp(id) {
    var len = $("#" + id + " tr").size();
    var index = len - 1;
    for (var i = 0; i < len; i++) {
        if ($("#" + id + " tr.trDataRow").eq(i).hasClass("trAjaxMouseover")) {
            $("#" + id + " tr.trDataRow").eq(i % len).removeClass("trAjaxMouseover");
            index = i - 1;
        }
    }
    return (index < 0) ? (len - 1) : (index % len);
}
